<?php
namespace app\common\model;

use think\Model;

class User extends Model
{
    // 登陆
    public function login($openid)
    {
        $token = randString(32);
        $info = $this->where(['openid'=>$openid])->find();
        // $avatar_url = '';
        // try {
            
        //     $url = "https://api.multiavatar.com/".md5(GetOutTradeNo()).".png";
        //     var_dump($url);die;
        //     $res = getImage($url, 'uploads/txs');
        //     $avatar_url = config('file_url').'/'.$res['save_path'];
        //     //code...
        // } catch (\Throwable $th) {
        //     //throw $th;
        //     $avatar_url = config('file_url').'/static/imgs/mot.png';
        // }
        
        $txs = [1,2,3,4,5,6];
        $num = $txs[rand (0,count ($txs)-1)];


        if(!$info){
            $data = [
                'openid' => $openid,
                'token' => $token,
                'nick_name' => createNickname(),
                'avatar_url' => config('file_url').'/static/imgs/txs/'.$num.'.png',
                'sex' => '未知',
                'job' => '未知'
            ];
            $this->save($data);
        } else {
            // 登录
            $this->where(['openid'=>$openid])->update(['token' => $token]);
        }
        return $token;
    }

}